/*
 * @Author: xushuhao
 * @Email: xshmouse@163.com
 * @Date: 2020-04-15 16:16:07
 * @Last Modified by: xushuhao
 * @Last Modified time: 2020-06-24 11:17:46
 * @Description: 应用启动页面
 */

import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_code_framework/base/base_main_tabbar.dart';
import 'dart:async';
import 'package:flutter_code_framework/route/route_transition.dart';
import 'package:flutter_code_framework/pages/login.dart';
import 'package:flutter_code_framework/utils/global.dart';

class StartupPage extends StatefulWidget {
  //在State里通过widget.args调用
  // final Object args;

  const StartupPage({Key key}) : super(key: key);

  StartupPageState createState() => StartupPageState();
}

class StartupPageState extends State<StartupPage> {
  static Map args = {'isDisplaySec': true, 'second': 1};

  int lastsed = 0;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
          child: Column(
        children: <Widget>[
          Expanded(
            child: Center(
              child: Image.asset('lib/assets/images/startup_page/app_name.png'),
            ),
          ),
          Container(
            constraints: BoxConstraints.expand(height: 200),
            child: Center(
              child: Image.asset('lib/assets/images/startup_page/ck_logo.png'),
            ),
          ),
        ],
      )),
    );
  }

  @override
  void initState() {
    super.initState();

    //开启倒计时
    lastsed = args['second'];
    //设置倒计时三秒后执行跳转方法
    var duration = new Duration(seconds: lastsed);
    new Future.delayed(duration, goToHomePage);

    var timerduration = new Duration(seconds: 1);

    new Timer.periodic(timerduration, (self) {
      if (lastsed > 0) {
        setState(() {
          lastsed -= 1;
        });
      } else {
        self.cancel();
      }
      // print('lasted = $lastsed');
    });
  }

  void goToHomePage() {
  //  UserData _user;

    //判断登录状态
  G.user.getString().then((value) =>_setUserData(value));
    //路由表 此处不能使用 G.pushNamed 因为没有基础基类
    // G.pushNamed('/demo_page', arguments: {'arg2': 23});
    //Navigator.of(context).pushNamed('/demo_page');
  }

  _setUserData(dynamic value){
    if(value!=null){
      G.user.init(json.decode(value.toString()));
      if(G.user.data.password!=null){
        return RouteTransition(BaseMainTabbarPage()).pushAndRemoveUntilTransRoute(context);
      }    
    }
    RouteTransition(Login()).pushAndRemoveUntilTransRoute(context);
  }
}
